<template>
  <view>
    <!-- 搜索组件  :radius="1"  @click事件监听函数，监听子组件传递过来的方法-->
    <my-search :bgcolor="'pink'" @click="goToSearch"></my-search>
    
    <view class="scroll-view-container">
      <!-- 左侧滑动区域-->
      <scroll-view scroll-y="true" :style="{height: vh + 'px'}" class="scroll-left">
        <!-- 添加一个空标签 -->
        <block v-for="(item,i) in cateList" :key='i'>
          <view :class="['left-scroll-view-item',active === i ? 'active':'']" @click="activeChange(i)">{{item.cat_name}}
          </view>
        </block>
      </scroll-view>
      <!-- 右侧滑动区域-->
      <scroll-view scroll-y="true" :style="{height: vh+'px'}" class="scroll-right" :scroll-top="scrollTop">
        <!-- 二级分类标题 -->
        <view v-for="(item2,index) in cateLevel2" :key='index'>
          <view class="right-lev2-title">/{{item2.cat_name}}/</view>

          <!-- 三级分类 -->
          <view class="right-lev3-list">
            <view class="right-lev3-box" v-for="(item3,i3) in item2.children" :key='i3' @click="gotoGoodsList(item3)">
              <image :src="item3.cat_icon" mode=""></image>
              <text>{{item3.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 当前设备可用的高度
        vh: 0,
        cateList: [],
        active: 0,
        // 二级分类
        cateLevel2: [],
        scrollTop:0  // 定义滚动条的位置
      };
    },
    /* 监听页面加载事件*/
    onLoad() {
      uni.getSystemInfo({
          success: (res) => {
            // console.log(res)
            this.vh = res.windowHeight - 50
          }
        }),
        this.getCateList()
    },
    methods: {
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get('/categories')
        // console.log(res)
        if (res.meta.status !== 200) return uni.$showMsg()
        this.cateList = res.message
        // 为二级分类赋值
        this.cateLevel2 = res.message[0].children
      },
      // 改变左侧导航条的选中状态
      activeChange(i) {
        this.active = i
        // 改变索引，重新获取数据
        this.cateLevel2 = this.cateList[i].children
        // 切换一级目录，重新渲染右侧导航条的位置
        this.scrollTop = this.scrollTop===0? 1: 0
      },
      // 点击商品，跳转到商品详情页面
      gotoGoodsList(i){
        uni.navigateTo({
          url:`/subpkg/goods_list/goods_list?cid=${i.cat_id}`
        })
      },
      // 搜索框事件
      goToSearch(){
        // console.log(11111111111)
        uni.navigateTo({
          'url':'/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
  .scroll-view-container {
    display: flex;

    // 左侧滚动条
    .scroll-left {
      /* 宽度固定，高度固定使用px*/
      width: 120px;

      .left-scroll-view-item {
        line-height: 60px;
        text-align: center;
        font-size: 12px;
        background-color: #f7f7f7;

        &.active {
          background-color: #ffffff;
          position: relative;

          &::before {
            content: ' ';
            display: block;
            width: 4px;
            height: 30px;
            background-color: #c00000;
            position: absolute;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
          }
        }
      }
    }

    // 右侧滚动条
    .scroll-right {

      // 标题部分
      .right-lev2-title {
        font-size: 12px;
        font-weight: bold;
        text-align: center;
        padding: 15px 0;
      }

      // 列表样式
      .right-lev3-list {
        display: flex;
        flex-wrap: wrap;

        .right-lev3-box {
          width: 33.33%;
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          margin-bottom: 10px;

          image {
            width: 60px;
            height: 60px;
          }

          text {
            font-size: 12px;
          }
        }
      }
    }
  }
</style>
